//
//  SignInWithAppleViewController.m
//  Study
//
//  Created by startiasoft on 2020/2/26.
//  Copyright © 2020 Startiasoft. All rights reserved.
//

#import "SignInWithAppleViewController.h"

// 封装苹果登录请求类
#import "SignInApple.h"
// 系统头文件
#import <AuthenticationServices/AuthenticationServices.h>

@interface SignInWithAppleViewController ()

// 防止被释放，回调方法不会走，或者也可以把SignInApple这个类写成单例
@property (nonatomic, strong) SignInApple *signInApple;

@end

@implementation SignInWithAppleViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"Sign In With Apple";

    self.view.backgroundColor = [UIColor whiteColor];
    
    
    /**
        Sign in with apple
        1.官方文档：https://developer.apple.com/sign-in-with-apple/
        2.官方文档：https://developer.apple.com/design/human-interface-guidelines/sign-in-with-apple/overview/introduction/
     
        3.集成文档：https://www.jianshu.com/p/e1284bd8c72a
     */
    [self createUI];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self perfomExistingAccount];
}

- (void)createUI{
    
    // 使用系统提供的按钮，要注意不支持系统版本的处理
    if (@available(iOS 13.0, *)) {
        // Sign In With Apple Button
        ASAuthorizationAppleIDButton *appleIDBtn = [ASAuthorizationAppleIDButton buttonWithType:ASAuthorizationAppleIDButtonTypeSignIn style:ASAuthorizationAppleIDButtonStyleWhiteOutline];
        appleIDBtn.frame = CGRectMake(30, self.view.bounds.size.height - 180, self.view.bounds.size.width - 60, 50);
        appleIDBtn.center = self.view.center;
        //appleIDBtn.cornerRadius = 22.f;
        [appleIDBtn addTarget:self action:@selector(didAppleIDBtnClicked) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:appleIDBtn];
    }
    
    // 或者自己用UIButton实现按钮样式
    //UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //addBtn.frame = CGRectMake(30, 80, self.view.bounds.size.width - 60, 44);
    //addBtn.backgroundColor = [UIColor orangeColor];
    //[addBtn setTitle:@"Sign in with Apple" forState:UIControlStateNormal];
    //[addBtn addTarget:self action:@selector(didCustomBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    //[self.view addSubview:addBtn];
}

// 登录
- (void)didAppleIDBtnClicked API_AVAILABLE(ios(13.0)){
    
    // 封装Sign In with Apple 登录工具类，使用这个类时要把类对象设置为全局变量，或者直接把这个工具类做成单例，如果使用局部变量，和IAP支付工具类一样，会导致苹果回调不会执行

    self.signInApple = [[SignInApple alloc] init];

    self.signInApple.sendLoginInfo = ^(NSDictionary *loginDic, BOOL isHaveValue) {
        //apple login
        if (isHaveValue) {
            NSLog(@"返回 = %@",loginDic);
        }
    };
    [self.signInApple handleAuthorizationAppleIDButtonPress];
}


// 如果存在iCloud Keychain 凭证或者AppleID 凭证提示用户
- (void)perfomExistingAccount{
    // 封装Sign In with Apple 登录工具类，使用这个类时要把类对象设置为全局变量，或者直接把这个工具类做成单例，如果使用局部变量，和IAP支付工具类一样，会导致苹果回调不会执行
    self.signInApple = [[SignInApple alloc] init];
    [self.signInApple perfomExistingAccountSetupFlows];
}

@end
